#include <common.h>


extern void logo_disp_init_bl(void);
extern void logo_disp_set_bl(int brightness);

uint32_t readl(uint32_t addr)
{
	uint32_t value = *(volatile uint32_t*)addr;
	return value;
}

void writel(uint32_t value, uint32_t addr)
{
	*(volatile uint32_t*)addr = value;
}

void writels(uint32_t value, uint32_t addr)
{
	uint32_t tmp = readl(addr);
	tmp |= value;
	writel(tmp, addr);
}

void writelc(uint32_t value, uint32_t addr)
{
	uint32_t tmp = readl(addr);
	tmp &= (~value);
	writel(tmp, addr);
}

void writelr(uint32_t value, uint32_t addr, uint32_t start, uint32_t bit)
{
	uint32_t region = (0xFFFFFFFFU << ( start + bit )) | (0xFFFFFFFFULL >> ( 32 - start ));
	uint32_t tmp = readl(addr);
	tmp &= region;
	tmp |= (~region) & (value << start);
	writel(tmp, addr);
}
#if 0
void gpio_direction_output(unsigned int gpio_num)
{
	unsigned char group = gpio_num / 8;
	unsigned char offset = gpio_num % 8;
	unsigned long reg_base = 0x1B000000 + (group * 0x10000);
	u32 reg_val = 0;

	reg_val = readl(reg_base + 0x4);
	reg_val |= BIT(offset);
	writel(reg_val, reg_base + 0x4);//GIPO_DMSK 1: MASK

	reg_val = readl(reg_base + 0x8);
	reg_val |= BIT(offset);
	writel(reg_val, reg_base + 0x8);//GPIO_DIR 1:output 0:input
}

void gpio_set_value(unsigned int gpio_num, u32 val)
{
	unsigned char group = gpio_num / 8;
	unsigned char offset = gpio_num % 8;
	unsigned long reg_base = 0x1B000000 + (group * 0x10000);
	unsigned int  reg_val = 0;

	reg_val = readl(reg_base + 0x0);
	if (val)
		reg_val |= BIT(offset);
	else
		reg_val &= (u32)(~(BIT(offset)));
	writel(reg_val, reg_base + 0x0);//GPIO_DATA 1: high  0: low
}

unsigned int gpio_get_value(unsigned int gpio_num)
{
	unsigned char group = gpio_num / 8;
	unsigned char offset = gpio_num % 8;
	unsigned long reg_base = 0x1B000000 + (group * 0x10000);
	unsigned int  reg_val = 0;

	reg_val = readl(reg_base + 0x0);
	reg_val &= (u32)(BIT(offset));
	return reg_val;
}
#endif
static void logo_disp_lcm_pre_proc(void)
{	
#define REG_TOP_PIN_BASE    0x28200000
#define REG_GPIO7_7_PIN     (0x124)
#define REG_GPIOX_6_PIN     (0x11C)
#define REG_GPIO5_4_PIN     (0x100)
#define REG_GPIO5_6_PIN     (0x108)

#define MC_GPIO7_7	(7*8+7)
#define MC_GPIO5_4	(5*8+4)
#define MC_GPIO5_6	(5*8+6)
#define MC_GPIOX_6	(10*8+6)

#define REG_LCD_BKL_PIN		REG_GPIO7_7_PIN

	writel(1, REG_TOP_PIN_BASE + REG_LCD_BKL_PIN);
	gpio_direction_output(MC_GPIO7_7);
	gpio_set_value(MC_GPIO7_7, 1);

	/*3.3v 0x2820011C */
	writel(1, REG_TOP_PIN_BASE + REG_GPIO5_4_PIN);
	gpio_direction_output(MC_GPIO5_4);
	gpio_set_value(MC_GPIO5_4, 1);

	writel(1, REG_TOP_PIN_BASE + REG_GPIO5_6_PIN);
	gpio_direction_output(MC_GPIO5_6);
	gpio_set_value(MC_GPIO5_6, 1);

	/*1.8v 0x2820011C */
	writel(1, REG_TOP_PIN_BASE + REG_GPIOX_6_PIN);
	gpio_direction_output(MC_GPIOX_6);
	gpio_set_value(MC_GPIOX_6, 1);
}

static void boot_logo_disp(void)
{
	//extern void do_logo_disp_data(void *par, unsigned short *color);
	//do_logo_disp_data(NULL, NULL);
	extern int spi_lcm_init(void);
	spi_lcm_init();
}

// width / height / address must be 16 aligned
void boot_logo(void)
{
	printf("start boot_logo!\n");
	//logo_disp_lcm_pre_proc();
	boot_logo_disp();
	//logo_disp_init_bl();
	//logo_disp_set_bl(256);
	printf("end boot_logo!\n");
}
